<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品规格</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="4">
          <el-input
            v-model="queryInfo.query"
            placeholder="请输入内容"
            clearable
            @clear="getList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="handleSearch"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4" style="float: right">
          <el-button type="primary" icon="el-icon-plus" @click="create"
            >添加规格</el-button
          >
        </el-col>
      </el-row>
      <el-table
        :data="specs"
        border
        style="width: 100%"
        @sort-change="sortChange"
      >
        <el-table-column prop="id" label="编号" sortable="custom">
        </el-table-column>
        <el-table-column prop="name" label="规格名称"> </el-table-column>
        <el-table-column prop="label" label="规格标注名称"> </el-table-column>
        <el-table-column prop="specification_group_name" label="规格组名称">
        </el-table-column>
        <el-table-column prop="type_show" label="规格类型"> </el-table-column>
        <el-table-column prop="location_show" label="显示位置">
        </el-table-column>
        <el-table-column prop="value" label="规格值" width="300px">
        </el-table-column>
        <el-table-column prop="address" label="是否可搜索">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.is_search == 1">是</el-tag>
            <el-tag type="warning" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序"> </el-table-column>
        <el-table-column prop="created_at" label="添加时间" width="200px">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="top-start"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                circle
                @click="edit(scope.row)"
              />
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="top-start"
              @click="handleDelete(scope.row)"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="handleDelete(scope.row)"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <div>
        <Pagination
          :total="total"
          :page.sync="queryInfo.pageNum"
          :limit.sync="queryInfo.pageSize"
          class="pagination"
          @pagination="getList"
        />
      </div>
    </el-card>
    <el-dialog :title="optText[opt]" :visible.sync="dialogVisible" width="50%">
      <el-form
        :model="specForm"
        :rules="specRules"
        ref="specRef"
        label-width="120px"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item label="规格名称" prop="name">
          <el-input v-model="specForm.name"></el-input>
        </el-form-item>
        <el-form-item label="规格标注名称" prop="label">
          <el-input v-model="specForm.label"></el-input>
        </el-form-item>
        <el-form-item label="规格类型" prop="type">
          <el-select
            v-model="specForm.type"
            placeholder="请选择类型"
            clearable
            style="width: 160px"
          >
            <el-option :value="1" label="文本"></el-option>
            <el-option :value="2" label="单选"></el-option>
            <el-option :value="3" label="多选"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可搜索" prop="is_search">
          <el-radio-group v-model="specForm.is_search">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
          <el-alert
            title="设为可搜索，则可显示在筛选条件中;只有单选、多选设置才有效"
            type="warning"
          />
        </el-form-item>
        <el-form-item label="规格组" prop="specification_group_id">
          <el-select
            v-model="specForm.specification_group_id"
            placeholder="请选择规格组"
            clearable
            style="width: 160px"
          >
            <el-option
              v-for="item in specGroup"
              :value="item.id"
              :key="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示的位置" prop="location">
          <el-select
            v-model="specForm.location"
            placeholder="请选择显示位置"
            clearable
            style="width: 160px"
          >
            <el-option :value="0" label="规格参数页"></el-option>
            <el-option :value="1" label="详情页"></el-option>
            <el-option :value="2" label="都显示"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格值" prop="value">
          <el-input
            type="textarea"
            :autosize="{ minRows: 4 }"
            show-word-limit
            clearable
            placeholder="请输入内容"
            v-model="specForm.value"
          >
          </el-input>
          <el-alert title="多个规格，请用回车分割" type="warning" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="specForm.sort"></el-input>
          <el-alert title="排序值越小越靠前" type="warning" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="opt === 'create' ? handleCreate() : handleEdit()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getList, create, edit, destroy } from '@/api/spec.js'
import Pagination from '@/components/Pagination'
export default {
  name: 'Spec',
  components: { Pagination },
  data () {
    return {
      specs: [],
      specGroup: [],
      total: 0,
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 20,
        sort: ''
      },
      dialogVisible: false,
      opt: '',
      optText: {
        create: '新增',
        edit: '修改'
      },
      specForm: {},
      specRules: {
        name: [{ required: true, message: '请填写规格名称', trigger: 'blur' }],
        type: [
          { required: true, message: '请选择规格类型', trigger: 'change' }
        ],
        is_search: [
          { required: true, message: '请选择是否搜索', trigger: 'change' }
        ],
        specification_group_id: [
          { required: true, message: '请选择规格组', trigger: 'change' }
        ],
        location: [
          { required: true, message: '请选择显示位置', trigger: 'change' }
        ],
        sort: [
          { required: true, message: '请填写排序', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      getList(this.queryInfo).then(response => {
        const data = response.data.data
        this.specGroup = data.list.specGroup
        this.specs = data.list.specs
        this.total = data.total
      })
    },
    //搜索
    handleSearch () {
      this.queryInfo.pageNum = 1
      this.getList()
    },
    //添加页面
    create () {
      this.opt = 'create'
      this.dialogVisible = true
    },
    edit (row) {
      this.specForm = null
      this.specForm = row
      this.opt = 'edit'
      this.dialogVisible = true
    },
    //添加操作
    handleCreate () {
      this.$refs['specRef'].validate((valid) => {
        if (!valid) return false;
        create(this.specForm).then(() => {
          this.dialogVisible = false
          this.getList()
          this.$message.success('添加成功')
        }).catch(() => {

        })
      })

    },
    //编辑操作
    handleEdit () {
      this.$refs['specRef'].validate((valid) => {
        if (!valid) return false;
        edit(this.specForm).then(() => {
          this.dialogVisible = false
          this.getList()
          this.$message.success('修改成功')
        })
      })

    },
    //删除
    handleDelete (row) {
      this.$confirm('是否删除该内容?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        destroy(row.id).then(() => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    sortChange (data) {
      const { prop, order } = data
      if (order === 'ascending') {
        this.queryInfo.sort = '+' + prop
      } else {
        this.queryInfo.sort = '-' + prop
      }
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped></style>
